﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace PropertyFileConverter
{
    class Program
    {
        private const string MSBUILD_PROPERTIES_URN = "urn:sdc-microsoft-com:deployment:properties:v2.0";

        static void Main(string[] args)
        {
            XmlSerializer ser = new XmlSerializer(typeof(Properties));
            String convertedFileDir = Path.Combine(Environment.CurrentDirectory, "Converted");

            if (!Directory.Exists(convertedFileDir))
                Directory.CreateDirectory(convertedFileDir);

            foreach (string filePath in Directory.GetFiles(Environment.CurrentDirectory, "*.xml"))
            {
                XmlDocument doc = null;
                Dictionary<String, String> properties = new Dictionary<String, String>();

                doc = new XmlDocument();
                doc.Load(filePath);

                XmlNodeList list = doc.SelectNodes("//node()[text() != '']");

                Properties outputProperties = new Properties();

                foreach (XmlNode node in list)
                {
                    String propertyName = GetPropertyByNode(node);
                    if (properties.ContainsKey(propertyName))
                    {
                        System.Console.WriteLine(String.Format("Dublicate property is found: {0} in node: {1}", propertyName, node.Name));
                        continue;
                    }
                    properties.Add(propertyName, node.InnerText.Trim());
                }

                List<PropertiesProperty> pList = new List<PropertiesProperty>();

                foreach (String propertyName in properties.Keys)
                {
                    PropertiesProperty p = new PropertiesProperty();
                    p.Name = propertyName;
                    p.Value = properties[propertyName];
                    pList.Add(p);
                }

                outputProperties.Property = pList.ToArray();

                String convertedFilePath = Path.Combine(convertedFileDir, Path.GetFileName(filePath));
                using (System.Xml.XmlTextWriter writer = new XmlTextWriter(convertedFilePath, Encoding.UTF8))
                {
                    writer.Formatting = Formatting.Indented;
                    ser.Serialize(writer, outputProperties);
                }
            }
        }

        private static string GetPropertyByNode(XmlNode node)
        {
            StringBuilder st = new StringBuilder();

            return GetPropertyByNode(node, ref st);
        }


        private static String GetPropertyByNode(XmlNode node, ref StringBuilder st)
        {
            st.Insert(0, node.Name);
            st.Insert(0, ".");

            if (!node.ParentNode.Name.Equals("root"))
                return GetPropertyByNode(node.ParentNode, ref st);
            else
            {
                st.Remove(0, 1);
                return st.ToString().ToUpper();
            }
        }
    }
}
